package com.gnomon.epsos.model;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.opensaml.saml2.core.Assertion;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;

import com.gnomon.LiferayUtils;
import com.gnomon.epsos.service.EpsosHelperService;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.portlet.PortletProps;

import epsos.openncp.protocolterminator.ClientConnectorConsumer;
import epsos.openncp.protocolterminator.clientconnector.DocumentId;
import epsos.openncp.protocolterminator.clientconnector.EpsosDocument1;
import epsos.openncp.protocolterminator.clientconnector.GenericDocumentCode;

@ManagedBean
@RequestScoped
public class pdfModelBean implements Serializable {

private static final long serialVersionUID = 1L;
private byte[] pdf;
private static Logger log = Logger.getLogger("pdfModelBean");

public pdfModelBean() {
	pdf=getCDA();
}

public byte[] getCDA() {

	byte[] pdf = null;
	log.info("getting pdf document");
	try {
		EpsosDocument selectedEpsosDocument = new EpsosDocument();
		String serviceUrl = com.liferay.util.portlet.PortletProps
				.get("client.connector.service.url");
		ClientConnectorConsumer clientConectorConsumer = new ClientConnectorConsumer(
				serviceUrl);

		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		PortletRequest portletRequest = (PortletRequest) externalContext
				.getRequest();
		HttpServletRequest req = PortalUtil.getHttpServletRequest(portletRequest);

		Assertion hcpAssertion = (Assertion) LiferayUtils.getFromSession("hcpAssertion");
		Assertion trcAssertion = (Assertion) LiferayUtils.getFromSession("trcAssertion");
		String selectedCountry = (String) LiferayUtils.getFromSession("selectedCountry");
	
		String uuid = portletRequest.getParameter("uuid");
		String repositoryId = portletRequest.getParameter("repositoryid");
		String hcid = portletRequest.getParameter("hcid");

		log.info("Retrieving PDF document");
		log.info("uuid: " + uuid);
		log.info("repositoryId: " + repositoryId);
		log.info("hcid: " + hcid);

		
		DocumentId documentId = DocumentId.Factory.newInstance();
		documentId.setDocumentUniqueId(uuid);
		documentId.setRepositoryUniqueId(repositoryId);
		
		GenericDocumentCode classCode = GenericDocumentCode.Factory.newInstance();
		String docType = portletRequest.getParameter("docType");
		if (docType.equals("ep"))
		{
		classCode.setNodeRepresentation(PortletProps.get("prescription.classCode"));
		classCode.setSchema(PortletProps.get("classcode.schema"));
		classCode.setValue(PortletProps.get("prescription.value"));
		}
		if (docType.equals("ps"))
		{
		classCode.setNodeRepresentation(PortletProps.get("patient.summary.classCode"));
		classCode.setSchema(PortletProps.get("patient.summary.schema"));
		classCode.setValue(PortletProps.get("patient.summary.value"));
		}

			
		EpsosDocument1 eps = clientConectorConsumer.retrieveDocument(
				hcpAssertion, trcAssertion, selectedCountry, documentId,
				hcid,classCode);
		selectedEpsosDocument.setAuthor(eps.getAuthor()+"");
		selectedEpsosDocument.setCreationDate(eps.getCreationDate());
		selectedEpsosDocument.setDescription(eps.getDescription());
		selectedEpsosDocument.setTitle(eps.getTitle());

		String xmlfile = new String(eps.getBase64Binary(), "UTF-8");
		
		pdf = EpsosHelperService.extractPdfPartOfDocument(xmlfile.getBytes());
		log.info("PDF SIZE: " + pdf.length);

	} catch (Exception ex) {
		ex.printStackTrace();
	}

	return pdf;
}

public byte[] getPdf() {
	return pdf;
}

public void setPdf(byte[] pdf) {
	this.pdf = pdf;
}







	
}
